package com.thx.common.service;

import com.thx.common.orm.model.ExtendPropertyEntity;
import com.thx.common.orm.model.Property;
import com.thx.common.orm.model.PropertyVal;
import com.thx.common.orm.model.Template;
import com.thx.common.util.DeepCopyUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * 扩展属性Manager，重写了BaseManager，实现了扩展属性相关表数据的维护，在开发中需要继承该Manager.
 *
 * @author calonlan
 */
@Transactional(readOnly = true)
public class ExtendPropertyManager<T extends ExtendPropertyEntity> extends BaseManager<T> {

  @Autowired
  protected ExtendPropertyComponent extendPropertyComponent;

  @Override
  public List<T> paginList(int start, int max) {
    List<T> list = super.paginList(start, max);
    // 查询模板、扩展属性及值
    if (list != null && list.size() > 0) {
      Template template =
          extendPropertyComponent.findTempalteByEntityClass(list.get(0).getClass().getName());
      if (template.getId() != null) {
        List<Property> properties = null;
        for (T entity : list) {
          properties =
              extendPropertyComponent.findPropertiesByTemplateIdAndEntityId(template.getId(),
                  entity.getId().toString());
          try {
            // 深度复制properties，否则每个实体对象的properties都是一样的
            properties = DeepCopyUtil.deepCopy(properties);
          } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
          } catch (IOException ex) {
            ex.printStackTrace();
          }
          entity.setProperties(properties);
          entity.setTemplate(template);
        }

      }
    }
    return list;
  }

  @Override
  @Transactional(readOnly = false)
  public void save(T entity) {
    super.save(entity);
    // 保存扩展属性值
    List<Property> properties = entity.getProperties();
    if (properties != null) {
      for (Property p : properties) {
        PropertyVal pv = p.getPropertyVal();
        pv.setEntityId(entity.getId().toString());
        extendPropertyComponent.savePropertyVal(pv);
      }
    }
  }

  @Override
  @Transactional(readOnly = false)
  public void update(T entity) {
    super.update(entity);
    // 更新扩展属性值
    List<Property> properties = entity.getProperties();
    if (properties != null) {
      for (Property p : properties) {
        PropertyVal pv = p.getPropertyVal();
        extendPropertyComponent.updatePropertyVal(pv);
      }
    }
  }

  @Override
  @Transactional(readOnly = false)
  public void delete(Serializable id) {
    super.delete(id);
    // 删除扩展属性的值
    extendPropertyComponent.delPropertyValByEntityId(id.toString());
  }

  @Override
  @Transactional(readOnly = false)
  public void delMulti(Serializable... ids) {
    // TODO Auto-generated method stub
    super.delMulti(ids);
    // 删除扩展属性的值
    for (Serializable id : ids) {
      extendPropertyComponent.delPropertyValByEntityId(id.toString());
    }
  }

  @Override
  public T get(Serializable id) {
    T entity = super.get(id);

    if (entity == null) {
      return null;
    }

    String entityClassName = entity.getClass().getName().toString();
    Template template = extendPropertyComponent.findTempalteByEntityClass(entityClassName);
    if (template.getId() != null) {
      entity.setTemplate(template);
      if (entity.getProperties() == null) {
        List<Property> properties = null;
        properties =
            extendPropertyComponent.findPropertiesByTemplateIdAndEntityId(template.getId(),
                id.toString());
        entity.setProperties(properties);
      }
    }
    return entity;
  }

  @Override
  public List<T> getAll() {
    List<T> list = super.getAll();
    // 查询模板、扩展属性及值
    if (list != null && list.size() > 0) {
      Template template =
          extendPropertyComponent.findTempalteByEntityClass(list.get(0).getClass().getName());

      if (template.getId() != null) {
        List<Property> properties = null;
        for (T entity : list) {
          properties =
              extendPropertyComponent.findPropertiesByTemplateIdAndEntityId(template.getId(),
                  entity.getId().toString());
          try {
            // 深度复制properties，否则每个实体对象的properties都是一样的
            properties = DeepCopyUtil.deepCopy(properties);
          } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
          } catch (IOException ex) {
            ex.printStackTrace();
          }
          entity.setProperties(properties);
          entity.setTemplate(template);
        }
      }
    }
    return list;
  }

}
